package com.mrbeard.greenbroswer.utils;

import com.mrbeard.greenbroswer.entites.Strategy;
import com.mrbeard.greenbroswer.entites.UrlList;

import java.text.SimpleDateFormat;
import java.util.Date;
import com.google.gson.Gson;
import com.mrbeard.greenbroswer.entites.Log;
import com.mrbeard.greenbroswer.entites.Strategy;
import com.mrbeard.greenbroswer.entites.UrlList;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 胡彬 on 2018/5/4.
 */
public class FormatUtil {

    public static String formatTimeStrategyToString(Strategy strategy) {
        String week = strategy.getWeek();
        String[] types = strategy.getType();
        String[] startTimes = strategy.getStartTimes();
        String[] endTimes = strategy.getEndTimes();




        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(week + "|");
        for (String typ : types) {
            int i = 0;
            stringBuilder.append(typ + ":start" + startTimes[i] + typ + ":end" + endTimes[i] + "|");
            i++;
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date[] beginDate = strategy.getBeginDate();
        Date[] endDate = strategy.getEndDate();
        for(int i = 0;i<beginDate.length;i++){
            stringBuilder.append("begin" + simpleDateFormat.format(beginDate[i]) + "over" + simpleDateFormat.format(beginDate[i]) + "|");
        }
        stringBuilder.append("\n");

        return stringBuilder.toString();
    }


    public static List<Strategy> formatStrategyFromJson(String jsonStrategy) {
        Gson gson = new Gson();
        List<Strategy> strategyList = new ArrayList<Strategy>();
        String[] str = jsonStrategy.split("");
        StringBuilder strbuilder = new StringBuilder();
        int startIndex = 0;
        int endIndex = 0;
        int p = 0;
        for (int i = 0; i < str.length; i++) {
            if (str[i].equals("{")) {
                startIndex = i-1;
            } else if (str[i].equals("}")) {
                endIndex = i;
                strbuilder.append(jsonStrategy.substring(startIndex, endIndex) + "#");
                p++;
            }
        }
        String[] sp2 = strbuilder.toString().split("#");
        for (int i = 0; i < sp2.length; i++) {
            //System.out.print(sp2[i].toString());
            Strategy strategy = gson.fromJson(sp2[i], Strategy.class);
            strategyList.add(strategy);
        }
        return strategyList;
    }

    public static String formatUrlListToString(UrlList urlList) {
        String jurisdiction = urlList.getJurisdiction();
        String content = urlList.getContent();

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[" + jurisdiction + "]" + "\n");
        stringBuilder.append(content+"\n");

        return stringBuilder.toString();
    }

    public static List<UrlList> formatUrlListFromJson(String urlJson) {
        Gson gson = new Gson();
        List<UrlList> urlListListList = new ArrayList<UrlList>();
        String[] str = urlJson.split("");
        StringBuilder strbuilder = new StringBuilder();
        int startIndex = 0;
        int endIndex = 0;
        int p = 0;
        for (int i = 0; i < str.length; i++) {
            if (str[i].equals("{")) {
                startIndex = i-1;
            } else if (str[i].equals("}")) {
                endIndex = i;
                strbuilder.append(urlJson.substring(startIndex, endIndex) + "#");
                p++;
            }
        }
        String[] sp2 = strbuilder.toString().split("#");
        for (int i = 0; i < sp2.length; i++) {
            //System.out.print(sp2[i].toString());
            UrlList urlList = gson.fromJson(sp2[i], UrlList.class);
            urlListListList.add(urlList);
        }
        return urlListListList;
    }


    public static List<Log> formatLogs(String logs,String deviceId) {
        List<Log> logList = new ArrayList<Log>();
        Log log = new Log();
        String[] splitString1 = logs.split("\n");
        for (int i = 0; i < splitString1.length; i++) {
            String[] splitString2 = splitString1[i].split(" ");
            StringBuilder stringBuilder = new StringBuilder();
            for (int j = 0; j < splitString2.length; j++) {
                if (j == 1) {
                    stringBuilder.append(splitString2[j] + " ");
                } else if (j == 2) {
                    stringBuilder.append(splitString2[j]);
                } else if (j == 4) {
                    log.setDesUrl(splitString2[j]);
                }
            }
            try {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Timestamp tm = new Timestamp(simpleDateFormat.parse(stringBuilder.toString()).getTime());
                log.setStartTime(tm);
                log.setDeviceId(deviceId);
                logList.add(log);
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }
        return logList;
    }
}
